package com.pda.yuanjing.pages.sybz;

import android.content.Context;

import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.beanin.DownBaseBeanIn;
import com.pda.yuanjing.beanin.ScanfBeanIn;
import com.pda.yuanjing.beanout.BDBatchBeanOut;
import com.pda.yuanjing.beanout.BDBeanOut;
import com.pda.yuanjing.beanout.BDMXBeanOut;
import com.pda.yuanjing.beanout.BaseBeanOut;
import com.pda.yuanjing.beanout.CodeQueryBeanOut;
import com.pda.yuanjing.beanout.ProductBeanOut;
import com.pda.yuanjing.beanout.StoreBeanOut;
import com.pda.yuanjing.beanout.TJBeanOut;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class PageModelImpl extends BaseModel implements PageModel {
    private String tag = "PageModelImpl";
    private BaseBeanOut baseBeanOut = null;
    private BDBeanOut bdBeanOut = null;
    private BDMXBeanOut bdmxBeanOut = null;
    private BDBatchBeanOut bdBatchBeanOut = null;

    public PageModelImpl(Context context) {
        super(context);
    }

    @Override
    public void queryBD(final CallBack callBack) {
        try {
            ScanfBeanIn scanfBeanIn = new ScanfBeanIn();
            scanfBeanIn.setAction("SY_FormAllQuery");
            scanfBeanIn.setCid(SharedPreferencesManger.getCid());
            scanfBeanIn.setUserID(SharedPreferencesManger.getUid());
            scanfBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(scanfBeanIn));
            api.downloadBillDeatail(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.ontipBD(bdBeanOut);
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (bdBeanOut != null && bdBeanOut.getErrmsg() != null) {
                                callBack.ontip(bdBeanOut.getErrmsg());
                            } else {
                                callBack.ontip(SetConfig.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            bdBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), BDBeanOut.class);
                            if (bdBeanOut != null && bdBeanOut.getErrcode() == 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(SetConfig.connectError);
            e.printStackTrace();
        }
    }

    @Override
    public void queryBDMX(String id, CallBack callBack) {
        try {
            ScanfBeanIn scanfBeanIn = new ScanfBeanIn();
            scanfBeanIn.setAction("SY_GetFormListByID");
            scanfBeanIn.setCid(SharedPreferencesManger.getCid());
            scanfBeanIn.setUserID(SharedPreferencesManger.getUid());
            scanfBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            scanfBeanIn.setFormID(id);
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(scanfBeanIn));
            api.downloadBillDeatail(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.ontipBDMX(bdmxBeanOut);
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (bdmxBeanOut != null && bdmxBeanOut.getErrmsg() != null) {
                                callBack.ontip(bdmxBeanOut.getErrmsg());
                            } else {
                                callBack.ontip(SetConfig.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            bdmxBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), BDMXBeanOut.class);
                            if (bdmxBeanOut != null && bdmxBeanOut.getErrcode() == 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(SetConfig.connectError);
            e.printStackTrace();
        }
    }

    @Override
    public void saveFormList(String id, BDMXBeanOut bdmxBeanOut, CallBack callBack) {
        try {
            ScanfBeanIn scanfBeanIn = new ScanfBeanIn();
            scanfBeanIn.setAction("SY_SaveFormList");
            scanfBeanIn.setCid(SharedPreferencesManger.getCid());
            scanfBeanIn.setUserID(SharedPreferencesManger.getUid());
            scanfBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            scanfBeanIn.setFormID(id);
            List<ScanfBeanIn.DataBean> list = new ArrayList<>();
            for (int i = 0; i <bdmxBeanOut.getData().size() ; i++) {
                ScanfBeanIn.DataBean temp = new ScanfBeanIn.DataBean();
                temp.setKey(bdmxBeanOut.getData().get(i).getKey());
                temp.setValue(bdmxBeanOut.getData().get(i).getValue());
                temp.setNeedWrite(bdmxBeanOut.getData().get(i).isNeedWrite());
                list.add(temp);
            }
            scanfBeanIn.setListFormValue(list);
            LogUtils.w(scanfBeanIn);
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(scanfBeanIn));
            api.downloadBillDeatail(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.ontipBDBatch(bdBatchBeanOut);
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (bdBatchBeanOut != null && bdBatchBeanOut.getErrmsg() != null) {
                                callBack.ontip(bdBatchBeanOut.getErrmsg());
                            } else {
                                callBack.ontip(SetConfig.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            bdBatchBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), BDBatchBeanOut.class);
                            if (bdBatchBeanOut != null && bdBatchBeanOut.getErrcode() == 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(SetConfig.connectError);
            e.printStackTrace();
        }
    }
}